package fun.flyee.android.pengshan;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import com.tencent.bugly.crashreport.CrashReport;

import java.lang.ref.WeakReference;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.pengshan.social.BuildConfig;

public class AppContex extends Application {

    private static AppContex mContext;

    private WeakReference<Activity> mCurrentActivity;

    public static AppContex getContext() {
        return mContext;
    }

    public void attachBaseContext(Context ctx) {
        super.attachBaseContext(ctx);
        mContext = this;
    }

    public void onCreate() {
        super.onCreate();
        CrashReport.initCrashReport(getApplicationContext(), Systems.getAppMetaData(this, "com.tencent.bugly.appid"), BuildConfig.DEBUG);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                mCurrentActivity = new WeakReference<>(activity);
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                if (mCurrentActivity != null && mCurrentActivity.get() != null) {
                    mCurrentActivity.clear();
                    mCurrentActivity = null;
                }
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    public void onTerminate(Context context) {

    }

    public Activity getCurrAct() {
        if (mCurrentActivity != null && mCurrentActivity.get() != null) {
            return mCurrentActivity.get();
        }
        return null;
    }

}
